//
//  LYPublicCompletedViewController.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/31.
//

import UIKit

class LYPublicCompletedViewController: LYAuthBaseViewController {
    var name: String?
    var IDNum: String?
    var birthday: String?
    var onomit: Onomit?
    var productId: Int?
    var orderNum: String?
    private let contentImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        iv.image = UIImage(named: "public_completed_content")
        return iv
    }()
    
    lazy var okButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle(LYlocalizedString("OK"), for: .normal)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 27.5
        button.addTarget(self, action: #selector(okAction(_ :)), for: .touchUpInside)
        button.backgroundColor = UIColor(hex: "#1E4FF3")
        button.layer.borderColor = UIColor(hex: "#FFFFFF").cgColor
        button.layer.borderWidth = 1.0
        return button
    }()
    
    let infoView = LYThreeLineDashedView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        if appCountryEnv == .indonesia {
            self.bgImage = UIImage(named: "public_completed_bg_id")
        } else {
            self.bgImage = UIImage(named: "public_completed_bg")
        }
        
        commonInitSubviews()
        fetchData()
    }
    
    func fetchData() {
        guard let productId = productId else { return }
        LYHUDUtils.showLoading()
        LYStocksManager.fetchCardischoolalityInfo(productId: productId) { [weak self] cardData, msg, error in
            guard let self = self else { return }
            if error == nil {
                if let focusly = cardData?.focusly {
                    if let cardiarium = focusly.cardiarium,
                       cardiarium == 1 { // 身份证完成认证
                        if let generationth = focusly.generationth {
                            self.name = generationth.sarcling
                            self.IDNum = generationth.typo
                            self.birthday = generationth.agohood
                            self.updateInfoView()
                        }
                    }
                }
                
                LYHUDUtils.hideHUD()
            } else {
                
                LYHUDUtils.showToast(msg)
            }
        }
    }
    
    func commonInitSubviews() {
        let aspectRatio = 190.0 / 346.0
        let margin = 15.0
        let width = SCREEN_WIDTH - margin * 2
        let height = width * aspectRatio
        
        self.view.addSubview(contentImageView)
        self.view.addSubview(okButton)
        self.updateInfoView()
        self.view.addSubview(infoView)
        contentImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(width)
            make.height.equalTo(height)
        }
        
        infoView.snp.makeConstraints { make in
            make.edges.equalTo(contentImageView)
        }
        
        okButton.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(15)
            make.height.equalTo(55)
            make.top.equalTo(contentImageView.snp.bottom).offset(30)
        }
    }
    
    func updateInfoView() {
        infoView.configure(
            topTitle: LYlocalizedString("Real name") + " ",
            topContent: self.name ?? "",
            middleTitle: LYlocalizedString("ID number") + " ",
            middleContent: self.IDNum ?? "",
            bottomTitle: LYlocalizedString("Birthday") + " ",
            bottomContent: self.birthday ?? ""
        )
    }
  
    @objc func okAction(_ sender: UIButton) {
        NotificationCenter.default.post(name: .orderListFollowKey,
                                        object: nil,
                                        userInfo: ["customData": self.onomit as Any,
                                                   "orderNum": self.orderNum ?? ""])
    }
}
